<?php
/*CREATE TABLE IF NOT EXISTS `staffs` (
  `id` int(15) NOT NULL auto_increment,
  `username` varchar(255) NOT NULL,
  `passwd` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;*/

//todo: setters juist hashen

class User {

	var $username;
	var $passwd;
	var $hash;
	var $certificate;
	var $shortkey;
	var $account;
	var $wallet;
	
	var $usertable;
	
	//statische vars
	var $id;
	
	
	function getUsername() {
		return $this->username;
	}
	function getPasswd() {
		return $this->passwd;
	}
	function getUsertable() {
		return $this->usertable;
	}
	function getCertificate() {
		return $this->certificate;
	}
	function getShortKey() {
		return $this->shortkey;
	}
	function getAccount() {
		return $this->account;
	}
	function getWallet() {
		return $this->wallet;
	}
	function getHash() {
		return $this->hash;
	}
	function getId() {
		return $this->id;
	}
	
	function setUsername($username) {
		$this->username = $username;
	}
	function setPasswd($passwd) {
		$this->passwd = $passwd;
	}
	function setUsertable($usertable) {
		$this->usertable = $usertable;
	}
	function setCertificate($certificate) {
		$this->certificate = $certificate;
	}
	function setShortKey($shortkey) {
		$this->shortkey = $shortkey;
	}
	function setAccount($account) {
		$this->account = $account;
	}
	function setWallet($wallet) {
		$this->wallet = $wallet;
	}
	function setHash($hash) {
		$this->hash = $hash;
	}
	function setId($id) {
		$this->id = $id;
	}
	
	function save() {
		global $db;

		$sql = "INSERT INTO ". $this->usertable ." (username, passwd, certificate, shortkey, account, wallet, hash) VALUES (\"". $this->username ."\",\"". $this->passwd ."\",\"". $this->certificate ."\",\"". $this->shortkey ."\",\"". $this->account ."\",\"". $this->wallet ."\",\"". $this->hash ."\")";
		$result = $db->query($sql);
		if (DB::isError($result)) {
    		die ($result->getMessage());
		}
		$this->id = $this->getMaxId();
		return true;
	}
	
	function getMaxId() {
		global $db;
	
		return $db->getOne("SELECT max(id) FROM staff");
	}
	
	function validId() {
		global $db;
		
		$numrows = $db->getOne("SELECT count(*) FROM ". $this->usertable ." WHERE id=".$this->id);
		
		if($numrows==1)
			return true;
		else
			return false;
	}
	
	
	function initByUsername($username) {
		global $db;
		 
		$this->setUsername($username);
		
		if($this->username==null || $this->usernameExists($username,$this->usertable)==false)
			return false;
		$sql = "SELECT id, username, passwd, certificate, shortkey, account, wallet, hash FROM ".$this->usertable." WHERE username='".$this->username."'";
		$row = $db->getRow($sql);
		if (DB::isError($row)) {
			die ($row->getMessage());
		}
		
		$this->setId($row[0]);
		$this->setUsername($row[1]);
		$this->passwd = $row[2];
		$this->setCertificate($row[3]);
		$this->setShortKey($row[4]);
		$this->setAccount($row[5]);
		$this->setWallet($row[6]);
		$this->setHash($row[7]);
		
		return true;
	}
	
	function initByHash($hash) {
		global $db;
		 
		$this->setHash($hash);
		
		if($this->hash==null || $this->hashExists($hash,$this->usertable)==false)
			return false;
		$sql = "SELECT id, username, passwd, certificate, shortkey, account, wallet, hash FROM ".$this->usertable." WHERE hash='".$this->hash."'";
		$row = $db->getRow($sql);
		if (DB::isError($row)) {
			die ($row->getMessage());
		}
		
		$this->setId($row[0]);
		$this->setUsername($row[1]);
		$this->passwd = $row[2];
		$this->setCertificate($row[3]);
		$this->setShortKey($row[4]);
		$this->setAccount($row[5]);
		$this->setWallet($row[6]);
		$this->setHash($row[7]);
		
		return true;
	}
	
	function initById($id,$usertable) {
		global $db;
		 
		$this->setId($id);
		$this->setUsertable($usertable);
		
		if($this->id==null || $this->validId()==false)
			return false;
		$sql = "SELECT id, username, passwd, certificate, shortkey, account, wallet, hash FROM ".$this->usertable." WHERE id=".$this->id;
		$row = $db->getRow($sql);
		if (DB::isError($row)) {
			die ($row->getMessage());
		}
		
		$this->setUsername($row[1]);
		$this->passwd = $row[2];
		$this->setCertificate($row[3]);
		$this->setShortKey($row[4]);
		$this->setAccount($row[5]);
		$this->setWallet($row[6]);
		$this->setHash($row[7]);
		
		return true;
	}
	
	
	//Statische methode
	function getUserById($id,$usertable) {
		global $db;
	
		$sql = "SELECT id, username, passwd, certificate, shortkey, account, wallet, hash FROM ".$usertable." WHERE id=".$id;
		$row = $db->getRow($sql);
		if (DB::isError($row)) {
			die ($row->getMessage());
		}
		
		$user = new User;
		$user->setUsertable($usertable);
		$user->setId($row[0]);
		$user->setUsername($row[1]);
		$user->passwd = $row[2];
		$user->setCertificate($row[3]);
		$user->setShortKey($row[4]);
		$user->setAccount($row[5]);
		$user->setWallet($row[6]);
		$user->setHash($row[7]);
		
		return $user;
	}
	
	//statische methode
	function usernameExists($username,$usertable) {
		global $db;
		
		$numrows = $db->getOne("SELECT count(*) FROM ".$usertable." WHERE username='".$username."'");
		
		if($numrows==1)
			return true;
		else
			return false;
	}
	
	//statische methode
	function hashExists($hash,$usertable) {
		global $db;
		
		$numrows = $db->getOne("SELECT count(*) FROM ".$usertable." WHERE hash='".$hash."'");
		
		if($numrows==1)
			return true;
		else
			return false;
	}

}
